import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
import projects from '../static/project.json'
export default new Vuex.Store({
	state: {
		count: 0,
		projects: projects,
		tasktitle: 'aaa',
		taskId: 1008,
		key: 'all'
	},
	mutations: {
		//创建函数
		//不能执行异步操作
		add(state) {
			state.count++
		},
		minus(state) {
			state.count--
		},
		//传参
		addN(state, step) {
			state.count += step;
		},
		minusN(state, step) {
			state.count -= step;
		},
		setTaskContent(state, content) {
			state.tasktitle = content;
		},
		addTaskItem(state) {
			let obj = {
				"id": state.taskId,
				"tasktitle": state.tasktitle,
				"done": false
			}
			state.projects.push(obj);
			state.taskId++;
			state.tasktitle = "";
		},
		removeTaskItem(state, id) {
			const i = state.projects.findIndex(x => x.id == id);
			console.log(i);
			if (i !== -1) {
				state.projects.splice(i, 1);
			}
			console.log(state.projects)
		},
		//更改task状态
		changeChecked(state, param) {
			const i = state.projects.findIndex(x => x.id == param.id)
			if (i !== -1) {
				state.projects[i].done = param.status

			}
		},
		//未选中的赋值个projects
		cleanDone(state) {
			state.projects = state.projects.filter(x => x.done == false)
		},
		changeStatus(state, status) {
			state.key = status;
		}


	},
	getters: {
		showNum(state) {
			return '当前最新的数量:' + state.count;
		},
		unDoneLength(state) {
			return state.projects.filter(x => x.done == false).length;
		},
		projectList(state) {
			switch (state.key) {
				case 'all':
					return state.projects
					break;
				case 'done':
					return  state.projects.filter(x => x.done == true)
					break;
				default:
					return state.projects.filter(x => x.done == false)
					break;
			}
		}
	},
	actions: {
		//处理异步任务
		//通过触发mutations的方法进行触发,进行异步更新数据
		addAsync(context) {
			setTimeout(() => {
				context.commit('add')
			}, 10);
		},
		addAsyncN(context, step) {
			setTimeout(() => {
				context.commit('addN', step)
			}, 10);
		},



	}
})
